package com.example.shoppingdemo.Fragment;


import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.IdRes;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.shoppingdemo.R;
import com.example.shoppingdemo.ShoppingCart.CarAdapter;
import com.example.shoppingdemo.ShoppingCart.CarInfo;
import com.example.shoppingdemo.nslcommodity.CheckoutActivity;

import java.util.ArrayList;
import java.util.List;

/*
* 购物车*/
public class SpecialTopicFragment extends Fragment implements CarAdapter.CheckInterface,CarAdapter.ModifyCountInterface{
    private ListView MyList;
    private CheckBox allcheck;
    private TextView hejivalue,jiesuanvalue;
    private List<CarInfo> list;


    public static LinearLayout bottemLin;
    private CarAdapter adapter;

    private double totalPrice = 0.00;// 购买的商品总价
    private int totalCount = 0;// 购买的商品总数量

    private RelativeLayout jiesuanRea;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_special_topic, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable  Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initList();
        c();
    }
    private void initList() {
        list = new ArrayList<>();
        for(int i =0;i<10;i++){
            CarInfo info = new CarInfo();
            info.setSpnum("1");
            info.setSpprice("55");
            info.setSptype("黑;L");
            info.setSpname("夏季爆款.............");
            list.add(info);
        }
        adapter = new CarAdapter(requireContext(),list);
        MyList.setAdapter(adapter);
    }

    private void initView() {
        jiesuanRea =findViewById(R.id.jiesuanRea);
        bottemLin =findViewById(R.id.bottemLin);
        MyList =findViewById(R.id.MyList);
        allcheck =findViewById(R.id.allcheck);
        hejivalue  =findViewById(R.id.hejivalue);
        jiesuanvalue =findViewById(R.id.jiesuanvalue);
        allcheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doCheckAll();
            }
        });

        jiesuanRea.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(requireActivity(), CheckoutActivity.class);
                startActivity(intent);
            }
        });

    }
    private void c(){
        adapter.setCheckInterface(this);// 关键步骤1,设置复选框接口
        adapter.setModifyCountInterface(this);// 关键步骤2,设置数量增减接口
    }

    /** 全选与反选 */
    private void doCheckAll()
    {
        for (int i = 0; i < list.size(); i++)
        {
            list.get(i).setChoosed(allcheck.isChecked());
        }
        adapter.notifyDataSetChanged();
        calculate();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        jiesuanvalue = null;
        hejivalue = null;
        allcheck = null;
    }


    private boolean isAllCheck()
    {
        for (CarInfo info : list)
        {
            if (!info.isChoosed())
                return false;

        }

        return true;
    }
    //价格判断
    private void calculate() {
        totalCount = 0;
        totalPrice = 0.00;
        for (int j = 0; j < list.size(); j++) {
            CarInfo product = list.get(j);
            if (product.isChoosed()) {
                totalCount += Integer.parseInt(product.getSpnum());
                totalPrice += Double.parseDouble(product.getSpprice()) * Double.parseDouble(product.getSpnum());
            }
        }
        hejivalue.setText("¥" + totalPrice);
        jiesuanvalue.setText("" + totalCount);
    }

    @Override
    public void checkChild( int childPosition, boolean isChecked) {

        if (isAllCheck())
            allcheck.setChecked(true);
        else
            allcheck.setChecked(false);
        adapter.notifyDataSetChanged();
        calculate();
    }

    @Override
    public void doIncrease(int childPosition, View showCountView, boolean isChecked) {
        CarInfo info = (CarInfo) adapter.getItem(childPosition);
        int currentCount = Integer.parseInt(info.getSpnum());
        currentCount++;
        info.setSpnum(String.valueOf(currentCount));
        ((TextView) showCountView).setText(currentCount + "");

        adapter.notifyDataSetChanged();
        calculate();
    }

    @Override
    public void doDecrease( int childPosition, View showCountView, boolean isChecked) {
        CarInfo info = (CarInfo) adapter.getItem(childPosition);
        int currentCount = Integer.parseInt(info.getSpnum());
        if (currentCount == 1)
            return;
        currentCount--;

        info.setSpnum(String.valueOf(currentCount));
        ((TextView) showCountView).setText(currentCount + "");
        adapter.notifyDataSetChanged();
        calculate();

    }
    private <T extends View>T findViewById(@IdRes int resId)
    {
        return getView().findViewById(resId);
    }
}